package com.atao.baokede.controller;

import com.atao.baokede.pojo.Result;
import com.atao.baokede.pojo.Review;
import com.atao.baokede.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 评论相关接口
 */
@RestController
@RequestMapping("/reviews")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;


    /**
     * 添加主评论
     *
     * @param review 添加针对商品的直接评价（主评论）
     * @return 无
     */
    @PostMapping
    public Result addMainReview(@RequestBody Review review) {
        try {
            reviewService.addMainReview(review);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     *
     * 添加回复评论
     * @param review 回复已有的评价
     * @return 无
     */
    @PostMapping("/reply")
    public Result addReplyReview(@RequestBody Review review) {
        try {
            reviewService.addReplyReview(review);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     *
    * 获取指定商品的所有主评论
     * @param itemId 商品ID
     * @return 评论reviews
     */
    @GetMapping
    public Result<List<Review>> getReviewsByItemId(@RequestParam Long itemId) {
        try {
            List<Review> reviews = reviewService.getReviewsByItemId(itemId);
            return Result.success(reviews);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户对指定商品的评论
     *
     * @param itemId 商品ID
     * @return 评论review
     */
    @GetMapping("/current")
    public Result<Review> getReviewByCurrentUser(@RequestParam Long itemId) {
        try {
            Review review = reviewService.getReviewByUserIdAndItemId(itemId);
            return Result.success(review);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取评论的回复
     * 获取指定评价的所有回复
     *
     * @param parentId 父评价ID
     * @return 回复 replies
     */
    @GetMapping("/replies")
    public Result<List<Review>> getRepliesByParentId(@RequestParam Long parentId) {
        try {
            List<Review> replies = reviewService.getRepliesByParentId(parentId);
            return Result.success(replies);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除评论
     * 删除指定评价
     *
     * @param id 评价ID
     * @return 无
     */
    @DeleteMapping("/{id}")
    public Result deleteReview(@PathVariable Long id) {
        try {
            reviewService.deleteReview(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新评论
     *
     * @param id 评价ID
     * @param review 评论信息类
     * @return 无
     */
    @PutMapping("/{id}")
    public Result updateReview(@PathVariable Long id, @RequestBody Review review) {
        try {
            review.setId(id);
            reviewService.updateReview(review);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取商品平均评分
     *
     * @param itemId 商品ID
     * @return 平均评分averageScore
     */
    @GetMapping("/average")
    public Result<Double> getAverageScoreByItemId(@RequestParam Long itemId) {
        try {
            Double averageScore = reviewService.getAverageScoreByItemId(itemId);
            return Result.success(averageScore);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取商品评分分布
     * 计算指定商品的平均评分
     *
     * @param itemId 商品ID
     * @return 描述 distribution
     */
    @GetMapping("/distribution")
    public Result<Map<Byte, Long>> getScoreDistributionByItemId(@RequestParam Long itemId) {
        try {
            //key为评分值(1-5分)，value为对应评分的数量
            Map<Byte, Long> distribution = reviewService.getScoreDistributionByItemId(itemId);
            return Result.success(distribution);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
